﻿#include "MaterialJohnsonCookDamageWidget.h"
#include "ui_MaterialJohnsonCookDamageWidget.h"

#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterialJohnsonCookDamage.h"

#include <QMessageBox>

namespace GUI
{
    MaterialJohnsonCookDamageWidget::MaterialJohnsonCookDamageWidget(EventOper::ParaWidgetInterfaceOperator * oper,Interface::FITKAbaMaterialJohnsonCookDamage * item, QWidget * parent) :
        MaterialWidgetBase(oper,parent), _materialItem(item)
    {
        _ui = new Ui::MaterialJohnsonCookDamageWidget();
        _ui->setupUi(this);
        _ui->tableWidget->addTableRowAndItem(0);
        //设置为单行模式
        _ui->tableWidget->setIsAloneRow(true);
        showValue();
    }

    MaterialJohnsonCookDamageWidget::~MaterialJohnsonCookDamageWidget()
    {
        if (_ui != nullptr)delete _ui;
    }

    void MaterialJohnsonCookDamageWidget::showValue()
    {
        if (_materialItem == nullptr)return;

        double d1 = _materialItem->getD1();
        _ui->tableWidget->item(0, 0)->setText(QString::number(d1));
        double d2 = _materialItem->getD2();
        _ui->tableWidget->item(0, 1)->setText(QString::number(d2));
        double d3 = _materialItem->getD3();
        _ui->tableWidget->item(0, 2)->setText(QString::number(d3));
        double d4 = _materialItem->getD4();
        _ui->tableWidget->item(0, 3)->setText(QString::number(d4));
        double d5 = _materialItem->getD5();
        _ui->tableWidget->item(0, 4)->setText(QString::number(d5));
        double meltingTemperature = _materialItem->getMeltingTemperature();
        _ui->tableWidget->item(0, 5)->setText(QString::number(meltingTemperature));
        double transitionTemperature = _materialItem->getTransitionTemperature();
        _ui->tableWidget->item(0, 6)->setText(QString::number(transitionTemperature));
        double referenceStrainRate = _materialItem->getReferenceStrainRate();
        _ui->tableWidget->item(0, 7)->setText(QString::number(referenceStrainRate));
    }

    bool MaterialJohnsonCookDamageWidget::checkValue()
    {
        auto checkValue = [=](QString str, int row, int column)->bool {
            //判断数据是否有效
            bool isOk = false;
            str.toDouble(&isOk);
            if (isOk == true)return true;

            QString message = QString(tr("Tabular data for at least one option or suboption\n has either blank or zero valued row(s).\n"));
            message = message + QString(tr("Error: Johnson-Cook Damage row: %1\n").arg(row + 1));
            message = message + QString(tr("Error: Johnson-Cook Damage column: %1\n").arg(column + 1));
            QMessageBox::warning(this, tr("StructualApp"), message);
            return false;
        };

        //强制失去焦点，否则获取数据有错误
        _ui->tableWidget->setEnabled(false);
        _ui->tableWidget->setEnabled(true);
        int colNum = _ui->tableWidget->columnCount();
        //Johnson-Cook Damage只有一行数据，不判断多行
        for (int j = 0; j < colNum; j++) {
            QString value = "";
            value = _ui->tableWidget->item(0, j)->text();
            if (!checkValue(value, 0, j))return false;
        }
        return true;
    }

    bool MaterialJohnsonCookDamageWidget::saveValue()
    {
        //强制失去焦点，否则获取数据有错误
        _ui->tableWidget->setEnabled(false);

        if (_materialItem == nullptr)return false;

        double d1 = _ui->tableWidget->item(0, 0)->text().toDouble();
        _materialItem->setD1(d1);
        double d2 = _ui->tableWidget->item(0, 1)->text().toDouble();
        _materialItem->setD2(d2);
        double d3 = _ui->tableWidget->item(0, 2)->text().toDouble();
        _materialItem->setD3(d3);
        double d4 = _ui->tableWidget->item(0, 3)->text().toDouble();
        _materialItem->setD4(d4);
        double d5 = _ui->tableWidget->item(0, 4)->text().toDouble();
        _materialItem->setD5(d5);
        double meltingTemperature = _ui->tableWidget->item(0, 5)->text().toDouble();
        _materialItem->setMeltingTemperature(meltingTemperature);
        double transitionTemperature = _ui->tableWidget->item(0, 6)->text().toDouble();
        _materialItem->setTransitionTemperature(transitionTemperature);
        double referenceStrainRate = _ui->tableWidget->item(0, 7)->text().toDouble();
        _materialItem->setReferenceStrainRate(referenceStrainRate);
        return true;
    }
}
